<template>
    <div>
        <el-card>
            <el-row :gutter="12">
                <el-col :span="6">
                    <el-input
                        placeholder="请输入分类名称"
                        class="input-with-select"
                        v-model="queryInfo.name"
                        clearable
                        @clear="getCategoryList"
                    >
                    </el-input>
                </el-col>

                <el-col :span="2">
                    <el-button type="primary" @click="getCategoryList"
                        >搜索</el-button
                    >
                </el-col>
                <el-col :span="2">
                    <el-button type="primary" @click="newCategory"
                        >添加分类</el-button
                    >
                </el-col>
            </el-row>
            <el-table :data="categoryList" border stripe>

                <el-table-column label="id" prop="id"></el-table-column>
                <el-table-column label="分类名称" prop="name"></el-table-column>
                <el-table-column label="排序">
                    <template slot-scope="scope">
                        <el-input
                            v-model="scope.row.sortNum"
                            placeholder="请输入内容"
                            @change="
                                setSortNum(scope.row.id, scope.row.sortNum)
                            "
                        ></el-input>
                    </template>
                </el-table-column>
                 <el-table-column label="子分类">
                    <template slot-scope="sc">
                        <el-button type="warning" plain size="mini" @click="querySubCategoryList(sc.row.id)">子分类</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="添加时间" prop="addTime"></el-table-column>

                <el-table-column label="编辑">
                    <template slot-scope="scope">
                        <el-button
                            type="primary"
                            icon="el-icon-edit"
                            size="mini"
                            @click="editCategory(scope.row)"
                        ></el-button>
                    </template>
                </el-table-column>
                <!-- 分页 -->
            </el-table>
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
            >
            </el-pagination>
        </el-card>
        <el-dialog title="修改分类" :visible.sync="addCategoryVisible" width="60%">
            <el-form :model="addCategoryForm" ref="addCategoryRef" :rules="addCategoryRules" label-width="150px">
                <el-form-item label="分类名称" prop="name">
                    <el-input
                        v-model="addCategoryForm.name"
                        class="form_ipt"
                    ></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="sortNum">
                    <el-input
                        v-model="addCategoryForm.sortNum"
                        class="form_ipt"
                    ></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addCategoryVisible = false">取 消</el-button>
                <el-button type="primary" @click="addCategory">确 定</el-button>
            </span>
        </el-dialog>
        <sub-category ref="subCategory"></sub-category>
    </div>
</template>

<script>
import MallApi from "@/api/Mall.js";
import SubCategory from './SubCategory.vue';
export default {
  components: { SubCategory },
    data() {
        return {
            queryInfo: {
                CategoryName: "",
                pagenum: 1, // 页码
                pagesize: 10, // 页大小
            },
            categoryList: [],
            total: 0,
            addCategoryVisible: false,
            addCategoryForm: {
                id: "",
                name: "",
                sortNum: ""
            },
            addCategoryRules: {
                CategoryName: [
                    {
                        required: true,
                        message: "请输入分类名字",
                        trigger: "change",
                    }
                ]
            }
        };
    },
    watch: {

    },
    created() {
        this.getCategoryList();
    },
    methods: {
        getCategoryList() {
          MallApi.getCategoryList(this.queryInfo).then((res) => {
                if (res.success) {
                    this.categoryList = res.data.categoryList;
                    this.total = res.data.page.total;
                } else {
                    return this.$message.error("获取分类列表失败");
                }
            });
        },
        handleSizeChange(pagesize) {
            this.queryInfo.pagenum = 1;
            this.queryInfo.pagesize = pagesize;
            this.getCategoryList();
        },
        // 监听pagenum的事件
        handleCurrentChange(pagenum) {
            this.queryInfo.pagenum = pagenum;
            this.getCategoryList();
        },
        newCategory() {
            this.addCategoryVisible = true;
            this.addCategoryForm = {
               id: "",
                name: "",
                mobile: "",
                contactPerson: "",
                telephone: "",
                address: ""
            };
        },
        editCategory(info) {
          MallApi.getCategoryDetail({
                id: info.id,
            }).then((res) => {
                if (res.success) {
                    this.addCategoryVisible = true;
                    this.addCategoryForm = JSON.parse(JSON.stringify(res.data));
                }
            });
        },

        // 添加
        addCategory() {
            this.$refs.addCategoryRef.validate((valid) => {
                if (valid) {
                  MallApi.saveCategory(this.addCategoryForm).then((res) => {
                        if (res.success) {
                            this.addCategoryVisible = false;
                            this.getCategoryList();
                        }
                    });
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
        setSortNum(id, sortNum) {
          MallApi.setCategorySortNum({
                id: id,
                sortNum: sortNum,
            }).then((res) => {
                if (res.success) {
                    this.getCategoryList();
                }
            });
        },
        querySubCategoryList(categoryId) {
            this.$refs.subCategory.showSubCategoryList(categoryId);
        }
    }
};
</script>

<style>
.form_ipt {
    width: 450px;
}
</style>
